layui.define(['table', 'form', 'upload', 'admin'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;

    table.render({
        elem: '#LAY-task-manage'
        , url: '/admin/task/getlist'
        , cols: [[
            {field: 'id', width: 70, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'type_txt', title: '任务类型', width: 100}
            , {field: 'weight', title: '权重', width: 80}
            , {field: 'name', title: '标题', width: 200}
            , {field: 'description', title: '规则', minWidth: 200}
            , {field: 'frequency', title: '条件次数', width: 90}
            , {field: 'exp', title: '奖励经验', width: 90}
            , {field: 'gold', title: '奖励钻石', width: 90}
            , {field: 'diamond', title: '奖励宝石', width: 100}
            // , {field: 'gift_title', title: '奖励礼物', width: 150}
            , {
                field: '', title: '最后修改人', width: 150, templet: function (d) {
                    return d.admin ? d.admin.name + '（ID:' + d.admin.id + '）' : '';
                },
            }
            //, {field: 'create_time', title: '添加时间', width: 170}
            //, {field: 'motified_time', title: '编辑时间', width: 170}
            , {title: '操作', width: 210, align: 'center', fixed: 'right', toolbar: '#table-tasklist-gift'}
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: {
            none: '暂无相关数据！'//默认无数据
        }
        , even: true
    });

    //监听工具条
    table.on('tool(LAY-task-manage)', function (obj) {
        if (obj.event === 'sestatus') {
            var confirm_text = obj.data.status == 1 ? '确定要启用吗？' : '确定要停用吗？';
            layer.confirm(confirm_text, function (index) {
                var status = obj.data.status == 0 ? 1 : 0;

                admin.req({
                    url: '/admin/task/set_status'
                    , data: {"id": obj.data.id, "status": status}
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-task-manage");
                            layer.msg(res.msg, {icon: 1});
                        }
                    }
                });
            });
        } else if (obj.event === 'edit') {
            var index = layer.open({
                type: 2
                , title: '编辑'
                , content: '/admin/task/edit?id=' + obj.data.id
                , maxmin: true
                , area: ['650px', '550px']
            });
            layer.full(index);
        } else if (obj.event === 'del') {
            layer.confirm('确定要删除吗?', function (index) {
                admin.req({
                    url: '/admin/task/del'
                    , data: {"id": obj.data.id}
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-task-manage");
                            layer.msg(res.msg, {icon: 1});
                        }
                    }
                });
            });
        }
    });

    ///////////////////////////////////////////////  add ////////////////////////////////////////////////

    form.on('submit(task-add)', function (obj) {
        var data = obj.field;
        //提交
        admin.req({
            url: '/admin/task/add_post'
            , data: data
            , success: function (res) {
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1});
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        parent.window.location.reload();
                    }, 1200);
                }
            }
        });
        return false;
    });

    form.on('submit(task-edit)', function (obj) {
        var data = obj.field;
        //提交
        admin.req({
            url: '/admin/task/edit_post'
            , data: data
            , success: function (res) {
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1});
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        parent.window.location.reload();
                    }, 1200);
                }
            }
        });
        return false;
    });

    // 任务领取记录
    table.render({
        elem: '#LAY-task-receive-manage'
        , url: '/admin/task/user_receive_list'
        , cols: [[
            {field: 'id', width: 70, title: 'ID', sort: true, fixed: 'left'}
            , {
                field: '', title: '任务类型', width: 150, templet: function (d) {
                    return d.task ? d.task.type_txt : '';
                }
            }
            , {
                field: '', title: '标题', templet: function (d) {
                    return d.task ? d.task.name : '';
                }
            }
            , {
                field: '', title: '条件次数', width: 90, templet: function (d) {
                    return d.task ? d.task.frequency : '';
                }
            }
            , {field: 'exp', title: '奖励经验', width: 90}
            , {field: 'gold', title: '奖励钻石', width: 90}
             , {field: 'diamond', title: '奖励宝石', width: 100}
            // , {field: 'gift_title', title: '奖励礼物', width: 150}
            , {field: 'uid', title: '领取人ID', width: 100}
            , {
                field: '', title: '领取人昵称', width: 110, templet: function (d) {
                    return d.user ? d.user.nick_name : '';
                }
            }
            , {field: 'create_time', title: '领取时间', width: 180}
            , {
                field: '', title: '状态', width: 90, templet: function (d) {
                    return d.is_received == 1 ? '已领取' : '未领取';
                }
            }
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });


    exports('task', {})
});

